import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { PositionHelper } from "utils/position/PositionHelper";

/**运输任务
 * 
 */ 
export class TransportTask<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     *  
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
       
        const creep = arg.entity;
        if(!creep.memory.taskid)
        {

            return []
        }
        const task =arg.task as ITask<ICreepTaskData<"transport">>;

        if(!task)
        {
            return []
        }
        const nodeMemory = creep.getNodeMemory();
        if(creep.store.getUsedCapacity(task.data.arg.ResourceType)>0)
        {
            nodeMemory.state = 1;
        }
        else
        {
            nodeMemory.state = 0;
        }


        if(nodeMemory.state)
        {
            // 存资源
            if(task.data.arg.To.id)
            {
                const store = Game.getObjectById(task.data.arg.To.id) as AnyStoreStructure;
                const type = task.data.arg.ResourceType;

                if((store.store.getFreeCapacity(type) as number)<creep.store.getUsedCapacity(type) )
                {
                    return [ActionArgHelper.actionMoveto(creep,{pos:store.pos,range:3},{})];
                }
                else
                {
                    if(PositionHelper.Distance(creep.pos, task.data.arg.To)>1)
                    {
                        return [ActionArgHelper.actionMoveto(creep,{pos:PositionHelper.Create(task.data.arg.To),range:1},{})]
                    }
                    else
                    {
                        if(task.data.arg.amount)
                        {
                            
                            let amount = Math.min(store.store.getFreeCapacity(type) as number,creep.store.getUsedCapacity(type));
                            let realnumber = Math.min(amount,task.data.arg.amount );
                            task.data.arg.amount-=realnumber;
                            return [ActionArgHelper.actionTransfer(creep,store,[{
                                resourceType:type,
                                amount:realnumber
                            }])];
                        }
                        else
                        {
                            task.data.arg.amount=0;
                            return [ActionArgHelper.actionTransfer(creep,store)];
                        }
                    }
                }
            }
            else
            {
                return [ActionArgHelper.actionDrop(creep,PositionHelper.Create(task.data.arg.To))];
            }
        }
        else
        {
            // 取资源
            if(PositionHelper.Distance(creep.pos, task.data.arg.From)>1)
            {
                return [ActionArgHelper.actionMoveto(creep,{pos:PositionHelper.Create(task.data.arg.From),range:1},{})]
            }
            if(task.data.arg.From.id)
            {
                const obj = Game.getObjectById(task.data.arg.From.id) as AnyStoreStructure;
                if(obj)
                {
                    return [ActionArgHelper.actionWithdraw(creep,obj)]
                }
            }
            else
            {
                const res = creep.room.lookForAt(LOOK_RESOURCES,PositionHelper.Create(task.data.arg.From)).filter(e=>e.resourceType ==  task.data.arg.ResourceType)[0];
                if(res)
                {
                    return [ActionArgHelper.actionPickup(creep,res)]
                }
            }
            
        }
 
        return [ ];
    }
}
